package cn.com.dhc.danlu.delegate.delegate.list;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.delegate.callback.IListDataCallback;
import cn.com.dhc.danlu.delegate.callback.IRefreshView;
import cn.com.dhc.danlu.dlware.net.BaseNetworkTaskManager;
import cn.com.dhc.danlu.dlware.net.GeneralBusinessObserver;
import cn.com.hd.mall.web.webservices.entity.BaseResponse;
import rx.Observable;
import rx.Subscription;

/**
 * author: 吴海洋(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2016-09-22 14:55<br/>
 *
 * <p>
 * 列表分页&异步任务&数据dispatch
 * </p>
 */
public abstract class BaseListDataDelegate <ViewBridge extends IListDataCallback> extends BaseNetworkTaskManager<ViewBridge> implements IRefreshView {
    protected int mPageSize = 20;
    private int mCurrentPage = 1;
    private boolean isCurrentLoad = false;


    protected int getCurrentPage() {
        return mCurrentPage;
    }

    /**
     * callback
     *
     * @param isRefresh
     *         true stand for drop-down refresh
     *         false pull to refresh
     */
    @Override
    public void onRefresh(boolean isRefresh) {
        fetchData(isRefresh);
    }

    /**
     * 外部调用请求列表
     *
     * @param isRefresh
     *         是否是下拉刷新
     */
    public void fetchData(final boolean isRefresh) {
        if (isCurrentLoad) {
            return;
        }
        isCurrentLoad = true;
        int page = mCurrentPage;
        if (isRefresh) {
            page = 1;
        }
        Observable originObs = createRequestObservable(page);
        Subscription subscribe = originObs.subscribe(new GeneralBusinessObserver() {
            @Override
            protected void onSuccess(BaseResponse model) {
                List datas = excavateList(model);
                if (null == datas) {
                    datas = new ArrayList();
                }
                if (!datas.isEmpty()) {
                    if (isRefresh) {
                        mCurrentPage = 2;
                    } else {
                        mCurrentPage++;
                    }
                }
                mViewBridge.onSuccess(datas, isRefresh);
                isCurrentLoad = false;
            }

            @Override
            protected void onAlertTips(String displayMessage) {
                mViewBridge.onError(displayMessage, isRefresh);
                isCurrentLoad = false;
            }
        });
        mCompositeSubscription.add(subscribe);
    }

    /**
     * 请求参数 wrap Observabel
     */
    protected abstract Observable createRequestObservable(int page);

    /**
     * 获取list
     */
    protected abstract List excavateList(BaseResponse model);

    public void setRefreshViewAttr(XRecyclerView recyclerView) {
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(recyclerView.getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.SysProgress);
        recyclerView.setHasFixedSize(true);
        recyclerView.setRefreshProgressStyle(ProgressStyle.SysProgress);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setArrowImageView(R.drawable.dl_downgrey);
    }

    private class WrapContentLinearLayoutManager extends LinearLayoutManager {

        WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        @Override
        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
            try {
                super.onLayoutChildren(recycler, state);
            } catch (IndexOutOfBoundsException ignored) {
            }
        }
    }
}